package com.onlyphoto.onlymall.mvp.presenter

import com.onlyphoto.onlymall.base.BaseNetworkResponse

import android.content.Context
import com.onlyphoto.onlymall.base.BaseEntity
import com.onlyphoto.onlymall.base.BaseObserver

import com.onlyphoto.onlymall.mvp.contract.CShop
import com.onlyphoto.onlymall.mvp.model.MShopImpl
import com.onlyphoto.onlymall.base.BasePresenter
import com.onlyphoto.onlymall.bean.ShopBean
import com.onlyphoto.onlymall.bean.request.UpdateShopBean
import com.onlyphoto.onlymall.bean.response.GoodsDetailsBean


class PShopImpl(mContext: Context, mView: CShop.IVMall) : BasePresenter<CShop.IVMall, MShopImpl>(mContext, mView, MShopImpl()), CShop.IPMall {
    override fun getShoppingCart() {
        if (!mView.isLogin()) {
            mView.showLogin()
            return
        }

        mModel.requestShoppingCart(object :BaseObserver<ArrayList<ShopBean>>(mContext.get()) {
            override fun onSuccess(data: ArrayList<ShopBean>?, msg: String?) {
                mView.updateView()
                if (data == null) {
                    mView.showEmpty()
                }
                mView.getShoppingCartSuccess(data!!)
            }

            override fun onRequestStart() {
            }

            override fun onCodeError(t: BaseNetworkResponse<ArrayList<ShopBean>>?) {
            }

            override fun onFailure(e: Throwable?) {
            }

            override fun onFinally() {
            }
        })
    }

    override fun updateShoppingCart(bean: UpdateShopBean) {
        mModel.updateShoppingCart(bean, object :BaseObserver<BaseEntity>(mContext.get()){
            override fun onSuccess(data: BaseEntity?, msg: String?) {
                mView.updateShoppingCartSuccess()
            }

            override fun onCodeError(t: BaseNetworkResponse<BaseEntity>?) {
                mView.updateShoppingCartFail()
            }

            override fun onFailure(e: Throwable?) {
                mView.updateShoppingCartFail()
            }

            override fun onRequestStart() {
            }

            override fun onFinally() {
            }

        })
    }

    override fun delete() {
        val bean= mView.getDeleteEntity()
        if (bean.cart_id == "") {
            mView.showMsg("请选择商品")
        }
        mModel.delete(bean, object :BaseObserver<BaseEntity>(mContext.get()){
            override fun onSuccess(data: BaseEntity?, msg: String?) {
                mView.deleteSuccess()
                mView.showMsg("删除成功")
            }

            override fun onCodeError(t: BaseNetworkResponse<BaseEntity>?) {
            }

            override fun onFailure(e: Throwable?) {
            }

            override fun onRequestStart() {
            }

            override fun onFinally() {
            }

        })
    }

    override fun getDetails(id: Int) {
        mModel.requestDetails(object :BaseObserver<GoodsDetailsBean>(mContext.get()){
            override fun onSuccess(data: GoodsDetailsBean?, msg: String?) {
                if (data == null) {
                    mView.showEmpty()
                }
                mView.getDetailsSuccess(data!!)
            }

            override fun onCodeError(t: BaseNetworkResponse<GoodsDetailsBean>?) {
            }

            override fun onFailure(e: Throwable?) {
            }

            override fun onRequestStart() {
                mView.showLoading()
            }

            override fun onFinally() {
                mView.hideLoading()
            }

        }, id)
    }
}
